package com.cj.common;

import com.cj.controller.BonusController;
import com.cj.controller.DepartmentController;
import com.cj.controller.EmployeeController;
import com.cj.controller.IndexController;
import com.cj.controller.PostController;
import com.cj.controller.PostSalaryController;
import com.cj.controller.SalaryRecordController;
import com.cj.controller.TimecardController;
import com.cj.interceptor.LoginInterceptor;
import com.cj.model.Bonus;
import com.cj.model.Department;
import com.cj.model.Employee;
import com.cj.model.Post;
import com.cj.model.PostSalary;
import com.cj.model.SalaryRecord;
import com.cj.model.Timecard;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.render.ViewType;

/**
 * API引导式配置
 */
public class SalaryConfig extends JFinalConfig {
	
	/**
	 * 配置常量
	 */
	public void configConstant(Constants me) {
		// 加载少量必要配置，随后可用getProperty(...)获取值
		loadPropertyFile("config.properties");
		me.setDevMode(getPropertyToBoolean("devMode", false));
		me.setViewType(ViewType.JSP);
		//设置view层根目录
		me.setBaseViewPath("/resources/view");
		//设置http 404错误跳转页面
		me.setError404View("/WEB-INF/view/error/404.html");
		me.setError401View("/WEB-INF/view/error/404.html");
		me.setError403View("/WEB-INF/view/error/404.html");
		me.setError500View("/WEB-INF/view/error/404.html");
		me.setErrorView(400, "/WEB-INF/view/error/404.html");
		//设置文件下载地址
		me.setFileRenderPath("/download");
		//设置Freemarker模板引擎模板文件的后缀名
		me.setFreeMarkerViewExtension(".ftl");
		//设置国际化资源
		//me.setI18n("i18nResourceBaseName");
		//设置上传文件默认保存路径根目录
		me.setUploadedFileSaveDirectory(PathKit.getWebRootPath() +"/resources/temp");
		//设置URL参数分隔符 http://localhost/demo/blog/1-3  1为文章分类ID 3为分页页码 
		me.setUrlParaSeparator("-");
	}
	
	/**
	 * 配置路由
	 */
	public void configRoute(Routes me) {
		me.add("/", IndexController.class);	// 第三个参数为该Controller的视图存放路径
		me.add("/employee", EmployeeController.class,"/");			// 第三个参数省略时默认与第一个参数值相同，在此即为 "/blog"
		me.add("/department", DepartmentController.class,"/");	
		me.add("/post", PostController.class,"/");	
		me.add("/timecard", TimecardController.class, "/");
		me.add("/bonus", BonusController.class, "/");
		me.add("/salaryRecord", SalaryRecordController.class, "/");
		me.add("/postSalary", PostSalaryController.class, "/");
	}
	
	/**
	 * 配置插件
	 */
	public void configPlugin(Plugins me) {
		// 配置C3p0数据库连接池插件
		C3p0Plugin c3p0Plugin = new C3p0Plugin(getProperty("jdbcUrl"), getProperty("user"), getProperty("password").trim());
		me.add(c3p0Plugin);
		
		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
		me.add(arp);
		arp.addMapping("employee", "empId", Employee.class);	
		arp.addMapping("post", "postId", Post.class);
		arp.addMapping("department", "deptId", Department.class);
		arp.addMapping("timecard", "cardId", Timecard.class);
		arp.addMapping("postsalary", "postSalaryId", PostSalary.class);
		arp.addMapping("bonus", "bonusId", Bonus.class);
		arp.addMapping("salaryrecord", "recordId", SalaryRecord.class);
		arp.addMapping("postSalary", "postSalaryId", PostSalary.class);
	}
	
	/**
	 * 配置全局拦截器
	 */
	public void configInterceptor(Interceptors me) {
		me.add(new LoginInterceptor());        
	}
	
	/**
	 * 配置处理器
	 */
	public void configHandler(Handlers me) {
		
	}
	
	/**
	 * 建议使用 JFinal 手册推荐的方式启动项目
	 * 运行此 main 方法可以启动项目，此main方法可以放置在任意的Class类定义中，不一定要放于此
	 */
	public static void main(String[] args) {
		JFinal.start("src/main/webapp", 80, "/", 5);
	}
}
